﻿using AnQinFrameWorkAPI.DataBase;
using AnQinFrameWorkAPI.Entity.Develop.DTO.DevelopProjectList;
using AnQinFrameWorkAPI.Entity.Develop.Entity;
using AnQinFrameWorkAPI.Entity.Develop.View.DevelopProjectList;
using AutoMapper;
using AutoMapper.QueryableExtensions;
using FrameWork.DataHelper;
using FrameWork.Entity;
using FrameWork.JWT;
using FrameWork.Request;
using Microsoft.EntityFrameworkCore;
using static AnQinFrameWorkAPI.Common.Enum.DevelopEnum;

namespace AnQinFrameWorkAPI.Service.Develop;

/// <summary>
///     开发管理-项目列表业务层
/// </summary>
public class DevelopProjectListService
{
    //数据库
    private readonly DatabaseContext _context;

    //自动映射
    private readonly IMapper _mapper;

    //请求人信息
    private readonly IUserContext _userContext;

    //构造注入
    public DevelopProjectListService(IUserContext userContext, DatabaseContext context, IMapper mapper)
    {
        _userContext = userContext;
        _context = context;
        _mapper = mapper;
    }

    #region 获取开发管理项目列表

    public async Task<RequestResponseModel> GetProjectList(DevelopProjectListSearchDTO developProjectListSearchDTO)
    {
        //获取筛选
        var query = _context.DevelopProjectList
            .ProjectTo<DevelopProjectListView>(_mapper.ConfigurationProvider)
            .AsNoTracking().WhereIF(a => a.Code.Contains(developProjectListSearchDTO.Code),
                !string.IsNullOrEmpty(developProjectListSearchDTO.Code))
            .WhereIF(a => a.Name.Contains(developProjectListSearchDTO.Name),
                !string.IsNullOrEmpty(developProjectListSearchDTO.Name))
            .WhereIF(a => a.CreateTime > developProjectListSearchDTO.StartTime,
                developProjectListSearchDTO.StartTime != null)
            .WhereIF(a => a.CreateTime < developProjectListSearchDTO.EndTime,
                developProjectListSearchDTO.EndTime != null)
            .Where(a => a.IsDelete == false);

        //返回列表
        var developProjectList = await query
            .OrderBy(a => a.CreateTime)
            .Page(developProjectListSearchDTO.PageIndex, developProjectListSearchDTO.PageSize)
            .ToListAsync();

        //总数据量
        var pageCount = await query.CountAsync();

        //返回数据
        var pageResult = new PageResult<DevelopProjectListView>(developProjectList, pageCount);
        return RequestResponse.OK(pageResult, "查询成功");
    }

    #endregion

    #region 新增开发管理项目

    public async Task<RequestResponseModel> AddDevelopProject(DevelopProjectListAddDTO developProjectListAddDTO)
    {
        //校验员工
        var staff = await Valid.ValidId(_context.Staff, _userContext.UserId);
        if (staff == null) return RequestResponse.BadRequest("员工不存在");

        //校验客户
        var developProjectCustomer =
            await Valid.ValidId(_context.DevelopProjectCustomer, developProjectListAddDTO.CustomerId);
        if (developProjectCustomer == null) return RequestResponse.BadRequest("项目客户不存在");

        //生成新数据
        var developProject = _mapper.Map<DevelopProjectListModel>(developProjectListAddDTO);

        developProject.Id = Guid.NewGuid();
        developProject.CreateStaffId = staff.Id.ToString();
        developProject.CreateStaffName = staff.Name;
        developProject.CustomerId = developProjectCustomer.Id.ToString();
        developProject.CustomerName = developProjectCustomer.Name;
        developProject.Status = StatusEnum.未开始;
        developProject.IsDelete = false;
        developProject.CreateTime = DateTime.Now;

        //新增数据
        _context.DevelopProjectList.Add(developProject);
        await _context.SaveChangesAsync();

        return RequestResponse.OK(developProject.Id, "新增成功");
    }

    #endregion

    #region 更新开发管理项目

    public async Task<RequestResponseModel> PutDevelopProject(DevelopProjectListUpdateDTO developProjectListUpdateDTO)
    {
        //校验数据
        var query = await Valid.ValidId(_context.DevelopProjectList, developProjectListUpdateDTO.Id);
        if (query == null) return RequestResponse.ErrorRequest("项目Id无效");

        //编辑数据
        var developProject = _mapper.Map(developProjectListUpdateDTO, query);

        //更新表
        _context.DevelopProjectList.Update(developProject);
        await _context.SaveChangesAsync();

        return RequestResponse.OK(developProjectListUpdateDTO.Id);
    }

    #endregion

    #region 删除开发管理项目

    public async Task<RequestResponseModel> DeleteDevelopProject(SingleIdDTO singleIdDTO)
    {
        //校验数据
        var query = await Valid.ValidId(_context.DevelopProjectList, singleIdDTO.Id);
        if (query == null) return RequestResponse.ErrorRequest("项目Id无效");
        ;

        query.IsDelete = true;

        //删除操作
        _context.DevelopProjectList.Update(query);

        //保存库
        await _context.SaveChangesAsync();
        return RequestResponse.OK(singleIdDTO.Id, "删除成功");
    }

    #endregion

    #region 批量删除开发管理项目

    public async Task<RequestResponseModel> DeleteArrayDevelopProject(IdArrayDTO idArrayDTO)
    {
        //校验单据
        var query = await _context.DevelopProjectList
            .WhereIF(a => idArrayDTO.Ids.Contains(a.Id.ToString()), idArrayDTO != null)
            .ToListAsync();

        if (query.Count != idArrayDTO.Ids.Count) return RequestResponse.ErrorRequest("项目Id无效");

        foreach (var item in query) item.IsDelete = true;
        ;

        //删除并保存
        _context.DevelopProjectList.UpdateRange(query);
        await _context.SaveChangesAsync();
        return RequestResponse.OK(idArrayDTO.Ids, "删除成功");
    }

    #endregion
}